package org.wu.framework.lazy.orm.core.config.prop;

import lombok.Data;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.core.config.enums.LazyDataSourceType;
import org.wu.framework.lazy.orm.core.source.adapter.SourceFactory;

import javax.sql.DataSource;


@Data
public abstract class AbstractLazyDataSourceAttribute implements LazyDataSourceAttribute {


    /**
     * 别名
     */
    private String alias = "master";


    /**
     * JDBC URL of the database.
     */
    private String url;

    /**
     * Login username of the database.
     */
    private String username;

    /**
     * Login password of the database.
     */
    private String password;

    /**
     * Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
     * <p>"com.mysql.cj.jdbc.Driver"</>
     */
    private String driverClassName ;
    /**
     * Fully qualified name of the connection pool implementation to use. By default, it
     * is auto-detected from the classpath.
     */
    private Class<? extends DataSource> type;
    /**
     * 数据库类型兼容建表
     */
    private LazyDataSourceType lazyDataSourceType;

    /**
     * 默认数据源通过 LazyDataSourceProxyUtils 加持
     */
    private boolean useProxy = true;



    /**
     * 选择 驱动
     *
     * @return 驱动
     */
    public String switchDriverClassName() {
        if (ObjectUtils.isEmpty(driverClassName)) {
            LazyDataSourceType lazyDataSourceType = getLazyDataSourceType();
            return lazyDataSourceType.getDriver();
        }
        return driverClassName;
    }

    public LazyDataSourceType getLazyDataSourceType() {
        if (ObjectUtils.isEmpty(lazyDataSourceType)) {
            this.lazyDataSourceType = SourceFactory.getLazyDataSourceType(url);
        }
        return lazyDataSourceType;
    }

    /**
     * 获取数据源
     *
     * @return 数据源
     */
    @Override
    public DataSource getDatasource() {
        if (SourceFactory.defaultLazyDataSourceType == null) {
            SourceFactory.defaultLazyDataSourceType = getLazyDataSourceType();
        }
        return doGetDatasource();
    }

    /**
     * 获取数据源
     *
     * @return 数据源
     */
    @Override
    public String getUrl() {
        if (ObjectUtils.isEmpty(url)) {
            throw new IllegalArgumentException("配置数据库url错误请正确配置url");
        }
        // 格式化url
        return SourceFactory.formatUrl(url);
    }

    public abstract DataSource doGetDatasource();

    /**
     * 选择数据源类型
     *
     * @return
     */
    public Class<? extends DataSource> switchType() {
        if (ObjectUtils.isEmpty(type)) {
            try {
                LazyDataSourceType dataSourceType = getLazyDataSourceType();
                String dataSourceClass = dataSourceType.getDataSourceClass();
                Class<?> clazz = Class.forName(dataSourceClass);
                return (Class<? extends DataSource>) clazz;
            } catch (Exception e) {
                e.printStackTrace();
                throw new IllegalArgumentException("无法初始化数据源，请配置数据源类型: spring.datasource.type='',当前配置：" + this);
            }
        }
        return type;
    }

}
